<template>
  <view class="container">
    <uni-datetime-picker
      mode="date"
      :value="currentDate"
      :start="startDate"
      :end="endDate"
      @change="dateChange"
    ></uni-datetime-picker>
  </view>
</template>

<script>
export default {
  data() {
    return {
      currentDate: this.getCurrentDate(), // 当前日期
      startDate: this.getCurrentDate('start'), // 起始日期，比如当前日期前两天
      endDate: this.getCurrentDate('end') // 结束日期，比如当前日期后四天
    };
  },
  methods: {
    getCurrentDate(type) {
      const date = new Date();
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      if (type === 'start') {
        year = year - 1; // 例如，设置起始日期为一年前
      } else if (type === 'end') {
        year = year + 1; // 例如，设置结束日期为一年后
      }
      month = month > 9 ? month : '0' + month;
      day = day > 9 ? day : '0' + day;
      return `${year}-${month}-${day}`;
    },
    dateChange(e) {
      this.currentDate = e.detail.value;
    }
  }
};
</script>

<style>
.container {
  padding: 20px;
}
</style>